package com.ftg.learn.bus.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.basicinfo_record.service.BasicInfoRecordService;
import com.ftg.learn.bus.user.mapper.user_mapper;
import com.ftg.learn.bus.user.pojo.user;
import com.ftg.learn.bus.userole_control.service.userole_control_service;
import com.ftg.learn.bus.util.MassageForReturn;
import com.ftg.learn.bus.util.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Service
public class user_service {
    @Autowired
    user_mapper um;

    @Autowired
    userole_control_service ucs;

    @Autowired
    BasicInfoRecordService birs;



    /**
     * 查一条
     * @param user_id  用户名
     * @return
     */
    public user queryone(String user_id) {
        return um.selectById(user_id);
    }

    /**
     * 修改
     * @param s    用户信息类
     * @return
     */
    public int update(user s) {
        if(s.getUser_id()==null || s.getUser_id().equals("") || s.getPassword()==null ||s.getPassword().equals("") || s.getSex()==null || s.getSex().equals("") || s.getName()==null || s.getName().equals("")  ||s.getAge()==null || s.getAge().equals("") || s.getContactway()==null || s.getContactway().equals("")){
            return MassageForReturn.user_login_By_NullPointerError;
        }
        if(um.selectById(s.getUser_id()) == null){
            return MassageForReturn.user_login_By_usernameIsNotExist;
        }

        String id = s.getUser_id();
        String name = s.getName();
        birs.BasicUpdateRecord(id,name);
        return um.update(s);
    }

    /**
     * 添加
     * @param s     用户信息类
     * @return
     */
    public int add(user s) {
        if(s.getUser_id()==null || s.getUser_id().equals("") || s.getPassword()==null ||s.getPassword().equals("") || s.getSex()==null || s.getSex().equals("") || s.getName()==null || s.getName().equals("")  ||s.getAge()==null || s.getAge().equals("") || s.getContactway()==null || s.getContactway().equals("")){
            return MassageForReturn.user_login_By_NullPointerError;
        }
        if(um.selectById(s.getUser_id())!=null){
            return MassageForReturn.user_login_By_usernameIsExist;
        }
        birs.BasicInsertRecord(s.getUser_id(),s.getName());
        return um.insert(s);
    }

    /**
     * 单删除
     * @param user_id   用户名
     * @return
     */
    public int delete(String user_id) {
        if(um.selectById(user_id)==null){
            return MassageForReturn.user_login_By_usernameIsNotExist;
        }
        ucs.deleteByUser_id(user_id);
        return um.deleteById(user_id);
    }


    /**
     * 分页查询
     * @param page          页数
     * @param limit         每页条目数
     * @param user_id       用户名     /模糊查询使用
     * @param name          姓名       /模糊查询使用
     * @return
     */
    public ResultType<user> query(int page,int limit,String user_id,String name){
        ResultType<user> rs = new ResultType<>();
        rs.setCode(0);
        IPage<user> ip = new Page<>(page,limit);
        QueryWrapper<user> qw=new QueryWrapper<>();
        qw.and(wrapper->wrapper.like("user_id",user_id).like("name",name));
        ip =  um.selectPage(ip,qw);
        rs.setData(ip.getRecords());
        rs.setCount(ip.getTotal());
        return rs;
    }

    /**
     * 登录
     * @param user_id       用户名
     * @param password      密码
     * @return
     */
    public ResultType<Map<String, Object>> login(String user_id , String password){
        List<Map<String, Object>> list=um.login(user_id,password);
        ResultType<Map<String, Object>> rs=new ResultType();
        rs.setData(list);
        rs.setCode(0);
        rs.setCount(list.size());
        return rs;
    }

    /**
     * 批量删除用户  同时删除角色表对应用户的角色
     * @param user_ids
     * @return
     */
    public int deletes(List<String> user_ids){
        if(user_ids ==null || user_ids.size()==0){
            return 0;
        }
        um.deleteForUserole_control(user_ids);
        return um.deleteBatchIds(user_ids);
    }
}
